package com.xuef.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
import org.springframework.data.elasticsearch.annotations.FieldType;

/**
 * Blog.
 */
@Document(indexName = "blog", type = "blog")
@XmlRootElement // MediaType 转为 XML
public class EsBlog implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id  // 主键
    private String id;

    @Field(index = FieldIndex.not_analyzed, type = FieldType.Long)
    private Integer blogId; // Blog 的 id

    @Field(type = FieldType.String, searchAnalyzer = "ik_max_word", analyzer = "ik_max_word")
    private String title;

    @Field(type = FieldType.String, searchAnalyzer = "ik_max_word", analyzer = "ik_max_word")
    private String summary;

    @Field(index = FieldIndex.not_analyzed, type = FieldType.String)  // 不做全文检索字段
    //@Field(type = FieldType.String, searchAnalyzer = "ik_max_word", analyzer = "ik_max_word")
    private String username;

    @Field(type = FieldType.String, searchAnalyzer = "ik_max_word", analyzer = "ik_max_word")
    private String content;

    @Field(index = FieldIndex.not_analyzed, type = FieldType.String)  // 不做全文检索字段
    private String avatar;

    @Field(index = FieldIndex.not_analyzed, type = FieldType.Date)  // 不做全文检索字段
    private Timestamp createTime;

    @Field(index = FieldIndex.not_analyzed, type = FieldType.Integer)  // 不做全文检索字段
    private Integer readSize = 0; // 访问量、阅读量
    @Field(index = FieldIndex.not_analyzed, type = FieldType.Integer)  // 不做全文检索字段
    private Integer commentSize = 0;  // 评论量
    @Field(index = FieldIndex.not_analyzed, type = FieldType.Integer)  // 不做全文检索字段
    private Integer voteSize = 0;  // 点赞量

    @Field(type = FieldType.String, searchAnalyzer = "ik_max_word", analyzer = "ik_max_word")
    private String tags;  // 标签

    protected EsBlog() {  // JPA 的规范要求无参构造函数；设为 protected 防止直接使用
    }

    public EsBlog(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public EsBlog(Integer blogId, String title, String summary, String content, String username, String avatar,Timestamp createTime,
                  Integer readSize,Integer commentSize, Integer voteSize , String tags) {
        this.blogId = blogId;
        this.title = title;
        this.summary = summary;
        this.content = content;
        this.username = username;
        this.avatar = avatar;
        this.createTime = createTime;
        this.readSize = readSize;
        this.commentSize = commentSize;
        this.voteSize = voteSize;
        this.tags = tags;
    }

    public EsBlog(Blog blog){
        this.blogId = blog.getId();
        this.title = blog.getTitle();
        this.summary = blog.getSummary();
        this.content = blog.getContent();
        this.username = blog.getUser().getUsername();
        this.avatar = blog.getUser().getHeadUrl();
        this.createTime = new Timestamp(new Date().getTime());
        this.readSize = blog.getReadCount();
        this.commentSize = blog.getCommentCount();
        this.voteSize = blog.getLikeCount();
        this.tags = blog.getTags();
    }

    public void update(Blog blog){
        this.blogId = blog.getId();
        this.title = blog.getTitle();
        this.summary = blog.getSummary();
        this.content = blog.getContent();
        this.username = blog.getUser().getUsername();
        this.avatar = blog.getUser().getHeadUrl();
        this.createTime = new Timestamp(blog.getCreateTime().getTime());
        this.readSize = blog.getReadCount();
        this.commentSize = blog.getCommentCount();
        this.voteSize = blog.getLikeCount();
        this.tags = blog.getTags();
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getBlogId() {
        return blogId;
    }

    public void setBlogId(Integer blogId) {
        this.blogId = blogId;
    }

    public void setCreateTime(Timestamp createTime) {
        this.createTime = createTime;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public Integer getReadSize() {
        return readSize;
    }

    public void setReadSize(Integer readSize) {
        this.readSize = readSize;
    }

    public Integer getCommentSize() {
        return commentSize;
    }

    public void setCommentSize(Integer commentSize) {
        this.commentSize = commentSize;
    }

    public Integer getVoteSize() {
        return voteSize;
    }

    public void setVoteSize(Integer voteSize) {
        this.voteSize = voteSize;
    }

    public String getTags() {
        return tags;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String toString() {
        return String.format(
                "User[id=%d, title='%s', content='%s']",
                blogId, title, content);
    }
}
